<?php

namespace backend\controllers;

use backend\models\User;
use common\components\Error;
use mdm\admin\models\searchs\AuthItem;
use backend\services\UserService;
use Yii;
use yii\data\Pagination;
use yii\rbac\Item;
use yii\filters\VerbFilter;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends AdminBaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * 执行页面action之前的代码
     * @param \yii\base\Action $action
     * @return bool
     * @throws \yii\web\BadRequestHttpException
     */
    public function beforeAction($action)
    {
        $this->service = new UserService();
        $this->editDef = 1;
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }


    /**
     * 视图变量
     * @param $view
     * @param $info
     * @return array
     */
    public function getViewVars($view, $info)
    {
        $res = [];
        switch ($view) {
            case 'edit':
                break;
        }
        return $res;
    }

    /**
     * 列表页
     * @return string|void
     */
    public function actionIndex()
    {
        $viewVar = [];
        $model = User::find();

        // 总数
        $count = $model->count();

        // 使用总数来创建一个分页对象
        $pagination = new Pagination(['totalCount' => $count]);

        // 使用分页对象来填充 limit 子句并取得文章数据
        $list = $model->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        $viewVar['list'] = $list;
        $viewVar['page'] = $pagination;
        return $this->render('index', $viewVar);
    }


    /**
     * 修改密码
     */
    public function actionModifyPass()
    {
        // 数据处理
        if (Yii::$app->request->isPost) {
            $resp = Error::tryCatch(function () {
                $data = Yii::$app->request->post();
                $data['User']['id'] = $this->userInfo['id'];
                $serv = new UserService();
                $serv->modifyPass($data);
                return $this->success('修改成功');
            }, function ($e) {
                $status = 'FAILED';
                $message = $e->getMessage();
                $file = $e->getFile();
                $line = $e->getLine();
                if (strpos($message, '|#|') !== false) {
                    $tmp = explode('|#|', $message);
                    $message = $tmp[0];
                    count($tmp) > 1 && $status = $tmp[1];
                }
                if (Yii::$app->request->isAjax) {

                    $trace = self::handleTrace($e->getTrace());
                    return array_merge(parent::ajaxReturn($message, $status), ['trace' => $trace, 'file' => $file, 'line' => $line]);
                } else {
                    Yii::$app->getSession()->setFlash('message', $message);
                }
            });
            if (Yii::$app->request->isAjax) {
                return $this->ajaxArray($resp);
            }

        }
        return $this->render('modify-pass');
    }
}
